import service from './config.service'

const request = (params) => {
    let url = params.url;
    let method = params.method || "get";
    let data = params.data || {};
    let header = {}
    if (method == "post") {
        header = {
            'Content-Type': 'application/json'
        };
    }

    // 获取本地token
    if (uni.getStorageSync("token")) {
        header['token'] = uni.getStorageSync("token");
    }

    if(uni.getStorageSync('addressId')){
        data.request_address_id = uni.getStorageSync('addressId');
    }

    return new Promise((resolve, reject) => {
        uni.request({
            url: service.apiUrl + url,
            method: method,
            header: header,
            data: data,
            success(response) {
                const res = response
                // 根据返回的状态码做出对应的操作
                //获取成功
                // console.log(res);
                switch (res.data.code) {
                    case 1:
                        resolve(res.data);
                        break;
                    case 0:
                        setTimeout(() => {
                            uni.showToast({
                                title: res.data.msg,
                                icon:'none'
                            });
                        });
                        break;
                    case 401:
                        uni.showModal({
                            title: "提示",
                            content: "请登录",
                            showCancel: false,
                            success(res) {
                                uni.navigateTo({
                                    url: "/pages/login/index",
                                })
                            },
                        });
                        break;
                    default:
                        uni.showToast({
                            title: '请重试...',
                            icon:'none'
                        })
                        break;
                }
                /*if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    uni.clearStorageSync()
                    switch (res.statusCode) {
                        case 401:
                            /!*uni.showModal({
                                title: "提示",
                                content: "请登录",
                                showCancel: false,
                                success(res) {
                                    uni.navigateTo({
                                        url: "/pages/login/index",
                                    })
                                },
                            });*!/
                            break;
                        default:
                            uni.showToast({
                                title: '请重试...',
                                icon:'none'
                            })
                            break;
                    }
                }*/
            },
            fail(err) {
                console.log(err)
                if (err.errMsg.indexOf('request:fail') !== -1) {
                    wx.showToast({
                        title: '网络异常',
                        icon: "error",
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: '未知异常',
                        duration: 2000
                    })
                }
                reject(err);

            },
            complete() {
                // 不管成功还是失败都会执行
                uni.hideLoading();
                uni.hideToast();
            }
        });
    }).catch((e) => {});
}

export default request
